package com.bpmn.system.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bpmn.common.exception.ServiceException;
import com.bpmn.common.utils.BeanUtil;
import com.bpmn.system.domain.SysUser;
import com.bpmn.system.domain.form.UserForm;
import com.bpmn.system.domain.query.UserQuery;
import com.bpmn.system.domain.vo.UserVo;
import com.bpmn.system.mapper.SysUserMapper;
import com.bpmn.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author rain
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public IPage<UserVo> listUser(UserQuery query) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(query.getUserName()), SysUser::getUserName, query.getUserName())
                .like(StringUtils.hasText(query.getNickName()), SysUser::getNickName, query.getNickName())
                .eq(query.getStatus() != null, SysUser::getStatus, query.getStatus())
                .orderByDesc(SysUser::getCreateTime);
        Page<SysUser> page = new Page<>(query.getPageNum(), query.getPageSize());
        Page<SysUser> userPage = this.page(page, wrapper);
        return userPage.convert(user -> BeanUtil.toBean(user, UserVo.class));
    }

    @Override
    public void addUser(UserForm user) {
        // 检查管理员账号是否已存在
        if (baseMapper.exists(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName, user.getUserName()))) {
            throw new ServiceException("管理员账号已存在");
        }
        SysUser sysUser = BeanUtil.toBean(user, SysUser.class);
        // 加密管理员密码
        sysUser.setPassword(BCrypt.hashpw(user.getPassword()));
        this.save(sysUser);
    }

    @Override
    public void updateUser(UserForm user) {
        SysUser sysUser = getById(user.getId());
        if (sysUser == null) {
            throw new ServiceException("应用不存在");
        }
        // 检查管理员账号是否被其他应用使用
        if (baseMapper.exists(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUserName, user.getUserName())
                .ne(SysUser::getId, user.getId()))) {
            throw new ServiceException("管理员账号已存在");
        }
        SysUser newUser = BeanUtil.toBean(user, SysUser.class);
        // 如果提供了新密码，则更新密码
        if (StrUtil.isNotBlank(user.getPassword())) {
            newUser.setPassword(BCrypt.hashpw(user.getPassword()));
        }
        this.updateById(newUser);
    }

    @Override
    public void deleteUser(String id) {
        this.removeById(id);
    }

    @Override
    public void resetPassword(String id, String password) {
        SysUser user = getById(id);
        if (user == null) {
            throw new ServiceException("应用不存在");
        }
        user.setPassword(BCrypt.hashpw(password));
        baseMapper.updateById(user);
    }

}